package com.hxkj.controller.wechat.ueditor.upload;

import com.hxkj.controller.wechat.ueditor.define.AppInfo;
import com.hxkj.controller.wechat.ueditor.define.BaseState;
import com.hxkj.controller.wechat.ueditor.define.State;
import com.integral.mall.file.po.SysFile;
import com.integral.mall.file.service.SysFileService;
import org.apache.commons.io.FileUtils;

import java.io.*;

public class StorageManager {



	public static final int BUFFER_SIZE = 8192;

	public StorageManager() {
	}

	public static State saveBinaryFile(byte[] data, String path) {
		File file = new File(path);
		State state = valid(file);
		if (!state.isSuccess()) {
			return state;
		}
		try {
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(file));
			bos.write(data);
			bos.flush();
			bos.close();
		} catch (IOException ioe) {
			return new BaseState(false, AppInfo.IO_ERROR);
		}
		state = new BaseState(true, file.getAbsolutePath());
		state.putInfo( "size", data.length );
		state.putInfo( "title", file.getName() );
		return state;
	}

	public State saveFileByInputStream(InputStream is, String path,
			long maxSize,  SysFileService sysFileService,String suffix,String savePath) {
		State state = null;
		File tmpFile = getTmpFile();
		try {
			state = saveTmpFile(tmpFile, path);
            SysFile sysFile =  sysFileService.upload(tmpFile.getName(),suffix, savePath,is);
            state.putInfo("filename",sysFile.getFileName());
			state.putInfo("url", path+ sysFile.getFilePath());
			state.putInfo("size", sysFile.getFileSize() ==null?tmpFile.length():sysFile.getFileSize());
			return state;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new BaseState(false, AppInfo.IO_ERROR);
	}

	public static State saveFileByInputStream(InputStream is, String path,String suffix , String savePath,SysFileService sysFileService) {
		State state = new BaseState(true);;
		try {
			SysFile sysFile =  sysFileService.upload(System.currentTimeMillis()+"",suffix, savePath,is);
			state.putInfo("filename",sysFile.getFileName());
			state.putInfo("url", path+ sysFile.getFilePath());
			state.putInfo("size", sysFile.getFileSize());
			return state;
		} catch (Exception e) {
		}
		return new BaseState(false, AppInfo.IO_ERROR);
	}

	private static File getTmpFile() {
		File tmpDir = FileUtils.getTempDirectory();
		String tmpFileName = (Math.random() * 10000 + "").replace(".", "");
		return new File(tmpDir, tmpFileName);
	}

	private static State saveTmpFile(File tmpFile, String path) {
		State state = null;
		File targetFile = new File(path);

		if (targetFile.canWrite()) {
			return new BaseState(false, AppInfo.PERMISSION_DENIED);
		}
		try {
			//FileUtils.moveFile(tmpFile, targetFile);
		} catch (Exception e) {
			return new BaseState(false, AppInfo.IO_ERROR);
		}

		state = new BaseState(true);
		state.putInfo( "size", targetFile.length() );
		state.putInfo( "title", targetFile.getName() );
		
		return state;
	}

	private static State valid(File file) {
		File parentPath = file.getParentFile();

		if ((!parentPath.exists()) && (!parentPath.mkdirs())) {
			return new BaseState(false, AppInfo.FAILED_CREATE_FILE);
		}

		if (!parentPath.canWrite()) {
			return new BaseState(false, AppInfo.PERMISSION_DENIED);
		}

		return new BaseState(true);
	}
}
